var apiHost = {
  baseUrl: "https://www.dayuncn.cn",
}
var apiPath = {
  //登录
  login: "/app/second/index/login",
  //注册
  register: "/app/second/index/register"
}

const wxRequest = function (params) {
  wx.showLoading({
    title: '加载中...',
  })

  let token = wx.getStorageSync('userInfo').token

  wx.request({
    url: apiHost.baseUrl + params.apiPath,
    method: params.method || 'GET',
    data: params.data || {},
    header: {
      'content-type': params.contentType || 'application/json;charset=UTF-8',
      'token': token || '',
    },
    success: function (res) {
      wx.hideLoading()
      wx.stopPullDownRefresh()

      params.success && params.success(res)
    },
    fail: function (res) {
      wx.hideLoading()
      wx.stopPullDownRefresh()

      params.fail && params.fail(res)
      wx.showToast({
        title: '网络异常...',
        icon: 'none'
      })
    },
    complete: function (res) {
      params.complete && params.complete(res)
      
      console.log(">>>> method: " + (params.method || 'GET') + "  "  + apiHost.url +  params.apiDetail + "?" + (JSON.stringify(params.data) || {}), res)
    }
  })


}

// module.exports 初始值为一个空对象 {}
// exports 是指向的 module.exports 的引用
// exports = module.exports = {}, exports和module.exports都指向一个引用地址{}
module.exports = {
  api: apiHost,
  apiPath: apiPath,
  wxRequest: wxRequest
}